package myauctioneer;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.stringtemplate.v4.ST;

public final class ArticleServlet extends DefaultServlet
{
	/**
	 * Show user account info.
	 */
	public void indexAction(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		ST rootTpl = this.app.getTemplate("root");
//CHANGE TO DEDICATED FUNCTION ... START		
		/**
		 * Check if logged in as customer,
		 * if YES, don't allow to create new customer
		 */
		Customer customer = new Customer(this.app);
		customer.load(request.getSession());

		boolean isLoggedIn=false;
		try {
			isLoggedIn=customer.isLoggedIn(request.getSession());
		}
		catch (Exception e /*ServletException se*/) {
			rootTpl.add("msg", e.toString());
		}
		if (!isLoggedIn) rootTpl.add("login","");		
//CHANGE TO DEDICATED FUNCTION ... END		
		ST tpl = this.app.getTemplate("account");

//HERE RE-	Customer customer = new Customer(this.app);
//HERE RE-	customer.load(request.getSession());

		rootTpl.add("title", "My account (DefaultServlet)");

		tpl.add("customer", customer);
		rootTpl.add("content", tpl);

		response.getWriter().println(rootTpl.render());
	}

	/**
	 * edit action.
	 */
	public void editAction(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		//
	}
	
	/**
	 * create action.
	 */
	public void createAction(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		//
	}
	/**
	 * validation of complete data
	 * before creating 'new Article'
	 * (called by 'createAction(...)')
	 */
	private boolean validateArticle(HttpServletRequest request) {
		return false;
	}
}
